package com.hb.core.pojo.project.section.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hb.core.pojo.OrganizeInfo;
import com.hb.core.pojo.RoleDataAuthEntity;
import com.hb.core.pojo.project.section.entity.SupplyBidSignup;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * describe:
 * 供应商标段报名表
 * @author zhongdl
 * @date 2021/09/26
 */
@Getter
@Setter
public class SupplyBidSignupDTO extends SupplyBidSignup {
    /**
     * 数据权限对象
     */
    private RoleDataAuthEntity dataAuth;

    @ApiModelProperty(value = "项目编号")
    private String projectCode;

    @ApiModelProperty(value = "项目名称")
    private String projectName;

    @ApiModelProperty(value = "方案(包)编号")
    private String sectionCode;

    @ApiModelProperty(value = "方案(包)名称")
    private String sectionName;

    @ApiModelProperty(value = "响应文件递交截止时间及开标时间")
    @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime bidOpeningTime;

    @ApiModelProperty(value = "是否报名 1: 报名成功, 2: 未报名, 3: 报名未通过 4 报名中")
    private String signupStatusName;

    @ApiModelProperty(value = "招标方式, 关联字典(招标, 询价等)")
    private Integer bidWay;

    @ApiModelProperty(value = "招标方式, 关联字典(招标, 询价等)")
    private String bidWayName;

    @ApiModelProperty(value = "标段状态, 关联字典")
    private String sectionStatus;

    @ApiModelProperty(value = "标段状态, 关联字典")
    private String sectionStatusName;

    @ApiModelProperty(value = "是否缴纳保证金")
    private String bondFlagName;

    @ApiModelProperty(value = "招标方式: 1: 公开, 2: 邀请")
    private Integer commonType;

    @ApiModelProperty(value = "文件发售开始时间")
    @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime  documentSaleStartTime;

    @ApiModelProperty(value = "文件发售结束时间")
    @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime  documentSaleEndTime;

    @ApiModelProperty(value = "标段报名的标段信息")
    private List<SupplyBidSignupDTO> supplyBidSignupList;

    @ApiModelProperty(value = "报名开始时间")
    @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime  signStartTime;

    @ApiModelProperty(value = "报名结束时间")
    @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime  signEndTime;

    @ApiModelProperty(value = "公告标题")
    private String noticeTitle;

    @ApiModelProperty(value = "公告id")
    private Long noticeId;

    @ApiModelProperty(value = "开标时间")
    @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime  openBidTime;

    @ApiModelProperty(value = "文件售价")
    private BigDecimal documentSalePrice;

    @ApiModelProperty(value = "是否支付成功")
    private Integer payStatus;


    @ApiModelProperty(value = "招标文件id")
    private Long fileId;
    /**
     * @Description:  待确认的数量
     **/
    private Long beConfirmedCount;

    /**
     *
     * @Description: 不通过的数量
     **/
    private Long failCount;

    /**
     *
     * @Description: 通过的数量
     **/
    private Long adoptCount;

    /**
     *
     * @Description: 通过的数量
     **/
    private Long totalCount;

    /**
     *
     * @Description: 是否仅显示可参与的
     *              1 显示报名截止时间之前的公开 需要报名的项目
     *              2  显示所有的需要公开并且需要报名的项目
     **/
    private Integer participateFlag;


    @ApiModelProperty(value = "代理机构信息")
    private OrganizeInfo agencyOrganizeInfo;

    public  String paramsValid (){
//        if (null == this.getSupplyId()) {
//            return "供应商Id为空";
//        }
        if (null == this.getProjectId()) {
            return "项目Id为空";
        }
        if (null == this.getSectionId()) {
            return "标段Id为空";
        }
        if (StrUtil.isEmpty(this.getContactName())){
            return "联系人必填";
        }
        if (StrUtil.isEmpty(this.getMobile())){
            return "手机号必填";
        }
        if (this.getProjectId() == null){
            return "项目id不可为空";
        }
        /*if (CollectionUtils.isEmpty(this.getBidDocFile())){
            return "报名所需要材料不能为空";
        }
        if (CollectionUtils.isEmpty(this.getSupplyBidSignupList())){
            return "请至少勾选一个标段";
        }
        List<SupplyBidSignupDTO> supplyBidSignupList = this.getSupplyBidSignupList();*/

//        final NoticeService noticeService = SpringUtil.getBean(NoticeService.class);
//
//        for (SupplyBidSignupDto supplyBidSignup : supplyBidSignupList) {
//            Date noticeBidOpeningTimeBySectionId = noticeService.findNoticeBidOpeningTimeBySectionId(supplyBidSignup.getSectionId());
//            if (noticeBidOpeningTimeBySectionId.before(new Date())){
//                return "标段" + supplyBidSignup.getSectionName() +"报名截止时间已过";
//            }
//        }
        return null;
    }

    public static List<SupplyBidSignup> changeSupplyBidSignupArr(SupplyBidSignupDTO supplyBidSignup, Long projectId,String clientIpAddress){
        List<SupplyBidSignup> supplyBidSignupList = new ArrayList<>();
        List<SupplyBidSignupDTO> supplyBidSignupDtoListList = supplyBidSignup.getSupplyBidSignupList();
        if (CollectionUtils.isEmpty(supplyBidSignupDtoListList)){
            return supplyBidSignupList;
        }
//        final UserProvider userProvider = SpringUtil.getBean(UserProvider.class);
//        for (SupplyBidSignupDto supplyBidSignupDto : supplyBidSignupDtoListList) {
//            SupplyBidSignup supplyBidSignupResult = supplyBidSignupDto.to(SupplyBidSignup.class);
//            supplyBidSignupResult.setId(RandomUtil.uuId());
//            supplyBidSignupResult.setContactName(supplyBidSignup.getContactName());
//            supplyBidSignupResult.setTel(supplyBidSignup.getTel());
//            supplyBidSignupResult.setMobile(supplyBidSignup.getMobile());
//            supplyBidSignupResult.setNote(supplyBidSignup.getNote());
//            supplyBidSignupResult.setSignupStatus(1);
//            supplyBidSignupResult.setProjectId(projectId);
//            supplyBidSignupResult.setEmail(supplyBidSignupDto.getEmail());
//            supplyBidSignupResult.setBidDocFile(supplyBidSignup.getBidDocFile());
//            supplyBidSignupResult.setIpAddress(clientIpAddress);
//            supplyBidSignupResult.setSupplyId(StrUtil.isEmpty(supplyBidSignupDto.getSupplyId()) ? userProvider.get().getOrganizeId() : supplyBidSignupDto.getSupplyId());
//            supplyBidSignupList.add(supplyBidSignupResult);
//        }
        return supplyBidSignupList;
    }

    public String paramsConfirmValid() {
//        if (StrUtil.isEmpty(this.getInviteId())){
//            return "邀请id不可为空";
//        }
//        if (StrUtil.isEmpty(this.getTel())){
//            return "回执电话必填";
//        }
        return null;
    }

    /**
     * SupplyBidSignup -> SupplyBidSignupDto
     *
     * @param supplyBidSignups
     * @return
     */
    public static List<SupplyBidSignupDTO> changeSupplyBidSignupDto(List<SupplyBidSignup> supplyBidSignups){
        List<SupplyBidSignupDTO> list = CollUtil.list(false);
        if (CollUtil.isEmpty(supplyBidSignups)){
            return list;
        }
        for (SupplyBidSignup supplyBidSignup : supplyBidSignups) {
            list.add(supplyBidSignup.to(SupplyBidSignupDTO.class));
        }
        return list;
    }
}


